package com.woniuxy.lrz.carlproject.config;

import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class CrossFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        log.warn("req:{}",request.getRequestURI());
//        POST, GET, PUT, DELETE
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "*");
        if(StringUtil.equals(request.getMethod(),"OPTIONS")){
            response.setStatus(response.SC_OK);
            return;
        }
        log.info("filter in "+request.getRequestURL());
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}



